package com.ssfGame.puremvc.view.module.Game.vc
{
	import api.dataStruct.ReadStoreVO;
	import api.dataStruct.SaveStoreVO;
	import api.event.CallFunName;
	
	import com.game.api.APICore;
	import com.game.baseMc.BaseMc;
	import com.game.btnClass.BtnClass_bright;
	import com.game.data.GameStaticData;
	import com.game.utils.GlobalFun;
	import com.greensock.TweenLite;
	import com.ssfGame.common.Ball;
	import com.ssfGame.puremvc.StaticConst;
	import com.ssfGame.puremvc.view.mc.GameTestBtn;
	import com.ssfGame.puremvc.view.module.ModuleVC;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class GameVC extends ModuleVC
	{
		
		
		public static const USER_CHANGE:String="user_change"//用户离开或者加入
		public static const GAME_OVER:String="game_over"//游戏结束	
			
		private var _btnListName:Array=["结束游戏"]//,"购买道具","获取道具列表","使用道具","保存进度","读取进度"];
		
		private var _isHost:Boolean;
		private var _myBall:Ball;
		private var _userBall:Ball;
		
		public function GameVC()
		{
			StaticConst.apiCore.currClass=this//必须添加;
			APICore.getInstance().callAPIFun(CallFunName.A_SET_GAME_STATE,true);//通知API游戏开始
			init();
		}
		private function init():void{
			_isHost=StaticConst.myInfo.isHost;
			copyBtnList();
			initBall();
			initMouse();
		}
		
		public function gc():void{
			gcVC();
			StaticConst.appStage.removeEventListener(MouseEvent.MOUSE_DOWN,thisMouseDown);
		}
		private function initMouse():void{
			StaticConst.appStage.addEventListener(MouseEvent.MOUSE_DOWN,thisMouseDown);
		}
		private function thisMouseDown(e:MouseEvent):void{
			
			TweenLite.to(_myBall,0.5,{x:mouseX,y:mouseY});
			sendMouse({x:mouseX,y:mouseY});
		}
		
		private function initBall():void{
			if(_isHost){
				_myBall=new Ball(0xFF0000,StaticConst.myInfo.nick);
				_userBall=new Ball(0x00FF00,StaticConst.userInfo.nick);
				_myBall.x=500;
				_userBall.x=100
			}else{
				_myBall=new Ball(0x00FF00,StaticConst.myInfo.nick);
				_userBall=new Ball(0xFF0000,StaticConst.userInfo.nick);
				
				_myBall.x=100;
				_userBall.x=500
			}
			_myBall.y=_userBall.y=200;
			addDis(_myBall);
			addDis(_userBall);
		}
		private function copyBtnList():void{
			for(var i:uint=0;i<_btnListName.length;i++){
				var _btn:GameTestBtn=new  GameTestBtn();
				var _point:Point=GlobalFun.getListPoint(i,7,112,40);
				_btn.x=28+_point.x;
				_btn.y=42+_point.y;
				_btn.id=i;
				_btn.txt.text=_btnListName[i];
				this.addChild(_btn);
				_btnEventList.push(new BtnClass_bright(_btn,btnClick,null,null,true));
			}
		}
		private function btnClick(e:MouseEvent):void{
			var _btn:GameTestBtn=e.currentTarget as GameTestBtn;
			switch(_btn.id){
				case 0:
					setGameOver();
					break;
			}
		}
		//====================================
		//API调用游戏方法
		
		//用户离开
		public function  UseChange(data:*=null):void{
			gc();
			sendToMediator(USER_CHANGE);
		}
		//暂停游戏
		public function  stopGame(isStop:Boolean):void{
			
			if(isStop){
				stage.frameRate=GameStaticData.PAUSE_RATE;
			}else{
				stage.frameRate=StaticConst.configVO.FRAMERATE
			}
			
		}
		//====================================
		//游戏调用API方法
		//打开购买道具界面
		private function buyItim():void{
			StaticConst.apiCore.callAPIFun(CallFunName.A_BUY_ITIM);
		}
		//使用道具
		private function useItim():void{
			StaticConst.apiCore.callAPIFun(CallFunName.A_USE_ITIM);
		}
		//获取道具列表
		private function getItimList():void{
			StaticConst.apiCore.callAPIFun(CallFunName.A_GET_ONE_ITIM);
		}
		//保存数据
		private function setStore():void{
			StaticConst.apiCore.callAPIFun(CallFunName.A_SAVE_STORE,new SaveStoreVO("战绩1",{d:1,f:2,e:3},3,1));
		}
		//获取数据
		private function getStore():void{
			StaticConst.apiCore.callAPIFun(CallFunName.A_READ_STORE,new ReadStoreVO(3,1));
			
		}
		//====================================
		//游戏间通信方法
		private function setGameOver():void{
			if(StaticConst.isRobot){
				recGameOver();
			}else {
				StaticConst.apiCore.sendToGameFun("recGameOver",1,"游戏结束喽");
			}
		}
		public function recGameOver(data:*=null):void{
			gc();
			sendToMediator(GAME_OVER);
		}
		private function sendMouse(obj:Object):void{
			APICore.getInstance().sendToGameFun("recMouseDown",0,obj);
		}
		public function recMouseDown(data:*):void{
			TweenLite.to(_userBall,0.5,{x:data.x,y:data.y});
		}
	}
}